<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" :inline="true" size="small">
            <el-form-item>
              <el-input v-model="queryData.name" placeholder="姓名" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="list">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;display: flex;">
          <el-button v-has="50401" type="primary" @click="add">新增</el-button>
        </div>
        <el-table ref="multipleTable" :data="table_body" stripe border :max-height="tableHeight">
          <el-table-column prop="userName" label="登录账号" />
          <el-table-column prop="name" label="姓名" />
          <el-table-column prop="roleNames" label="角色" />
          <el-table-column prop="phone" label="联系方式" />
          <el-table-column prop="enable" label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.enable">
                启用
              </div>
              <div v-else>停用</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-has="50402" size="small" shape="circle" type="text" @click="edit(scope.row)">编辑</el-button>
              <el-button v-if="scope.row.enable" v-has="50403" size="small" shape="circle" type="warning" @click="disabled(scope.row)">停用</el-button>
              <el-button v-else v-has="50404" size="small" shape="circle" type="success" @click="enable(scope.row)">启用</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" style="float:right;" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <el-dialog :visible.sync="openModal" :title="drawerTitle" width="60%" :close-on-click-modal="false">
      <el-form :model="formData">
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="登录账号" label-position="top">
              <el-input v-model="formData.userName" placeholder="登录账号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码" label-position="top">
              <el-input v-model="formData.password" type="password" placeholder="******" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="名字" label-position="top">
              <el-input v-model="formData.name" placeholder="联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" label-position="top">
              <el-input v-model="formData.phone" placeholder="联系方式" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="角色" label-position="top">
              <el-select v-model="formData.roleIds" multiple placeholder="请选择角色" style="width:100%;">
                <el-option v-for="(option, index) in roles" :key="index" :value="option.id" :label="option.name" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用状态" label-position="top">
              <el-select v-model="formData.enable" style="width:100%;">
                <el-option value="true" label="启用" />
                <el-option value="false" label="停用" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-right: 8px" @click="openModal = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      openModal: false,
      table_body: [

      ],
      styles: {
        height: 'calc(100% - 55px)',
        overflow: 'auto',
        paddingBottom: '53px',
        position: 'static'
      },
      formDataDefault: {
        userName: '',
        password: '',
        phone: '',
        name: '',
        roleIds: [],
        enable: 'true'
      },
      formData: {
        userName: '',
        password: '',
        name: '',
        phone: '',
        roleIds: [],
        enable: 'true'
      },
      queryData: {
        pageSize: 50,
        currentPage: 0,
        name: ''
      },
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      drawerTitle: '新增',
      roles: ''
    }
  },
  created() {
    this.list()
    this.role()
  },
  mounted() {
  },
  methods: {
    list() {
      request({
        url: 'user/list',
        data: this.queryData,
        method: 'post'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.table_body = res.data.records
            this.pageTotal = res.data.total
          } else {
            this.pageTotal = 0
          }
        }
      })
    },
    role() {
      request({
        url: 'role/list',
        method: 'get'
      }).then(res => {
        if (res.success) {
          this.roles = res.data
        }
      })
    },
    submit() {
      request({
        url: 'user/add-edit',
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.openModal = false
          this.list()
        }
      })
    },
    edit(row) {
      this.drawerTitle = '编辑'
      this.formData.id = row.id
      for (const x in this.formData) {
        if (x === 'enable') {
          this.formData[x] = row[x] + ''
        } else {
          this.formData[x] = row[x]
        }
      }

      this.openModal = true
    },
    add() {
      for (const x in this.formData) {
        this.formData[x] = this.formDataDefault[x]
      }
      this.openModal = true
    },
    disabled(row) {
      request({
        url: 'user/disabled?id=' + row.id,
        method: 'GET'
      }).then(res => {
        if (res.success) {
          this.list()
        }
      })
    },
    enable(row) {
      request({
        url: 'user/enable?id=' + row.id,
        method: 'GET'
      }).then(res => {
        if (res.success) {
          this.list()
        }
      })
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }

  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 300
    },
    pageSize() {
      return this.pageSizeOpts[0]
    },

    ...mapState(//
      {
        customerType: state => state.base.customerType, //
        paymentTerm: state => state.base.paymentTerm,
        customerOptions: state => state.base.customerOptions
      }
    )

  }
}
</script>
<style>
    .demo-drawer-footer{
        width: 100%;
        position: absolute;
        bottom: 0;
        left: 0;
        border-top: 1px solid #e8e8e8;
        padding: 10px 16px;
        text-align: right;
        background: #fff;
    }

    .demo-upload-list{
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0,0,0,.2);
        margin-right: 4px;
    }
    .demo-upload-list img{
        width: 100%;
        height: 100%;
    }
    .demo-upload-list-cover{
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0,0,0,.6);
    }
    .demo-upload-list:hover .demo-upload-list-cover{
        display: block;
    }
    .demo-upload-list-cover i{
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
</style>
